define(function (require, exports, module) {
    var angular = require("angular");
    var ui = require('ui');
    var util = require('util');
    require("angular-ui");
    var dialog = require("dialog");
    var api_prefix = G_CONFIG["api_prefix"] + "/qmyx/para-setting/"; //api前缀

    angular.module('PublicCommissionSetting', [])
        .controller('PublicCommissionSettingController', ['$scope', '$http', function ($scope, $http) {



            //佣金发放模式选择
            var paymentType = function (newValue) {
                var data = $scope.data;

                var title = "佣金发放模式";
                var str = '<div>您切换佣金发放模式，会影响发放流程和操作方式。确认切换吗？';
                var d = dialog({
                    title: title,
                    content: str,
                    fixed: true,
                    width: 400,
                    height: 60,
                    okValue: "保存",
                    ok: function () {
                        var $content = this._$('content');
                        $http.post(api_prefix + "save-yjff",
                            {
                                id: data.yjff.id,
                                value: data.yjff.value
                            }
                        )
                            .success(function (res) {
                                if (res.retCode == 0) {
                                    ui.showSucTip();
                                } else {
                                    ui.showErrTip(res.errMsg);
                                }
                            });
                    },
                    cancel: function () {
                        $scope.data.yjff.value = newValue == '0' ? '1' : '0';
                        $scope.$applyAsync();
                    }
                });
                d.showModal();
            };
            //未开通代支付功能提示
            function notOpenIep() {
                var title = "提示";
                var str = '<div><p>您尚未开通代支付功能，无法切换佣金发放模式，如需开通代</p>'
                    + '<p>支付服务，请咨询明源云客实施顾问或联系400客服团队。</p></div>';
                var _this = this;
                var d = dialog({
                    title: title,
                    content: str,
                    fixed: true,
                    width: 400,
                    height: 60,
                    cancelValue: "关闭",
                    cancel: function () {

                    }
                });
                d.showModal();

            };

            //存在代发放积分佣金不能切换成待支付

            function queryCanChange(){
                var flag = false;
                $.ajax({
                    type: 'GET',
                    async:false,
                    url: G_CONFIG.api_prefix + "qmyx/para-setting/check-point-brokage",
                    success: function(re) {
                        if (re.retCode == 0 && re.data.czdffjfyj != 0) {
                            flag = true;
                        }
                    },
                    error:function(){
                        flag = true;
                    }
                });
               return flag;
            }

            $scope.yjffChange = function (value) {
                if (value === '1' && $scope.data.yjff.isOpen == 0) {
                    notOpenIep();

                    $scope.data.yjff.value = '0';
                } else {
                    if (value == 1 && $scope.data && queryCanChange() ) {//czdffjfyj存在代发放积分佣金
                        util.alert('存在未审定或未发放的佣金积分，须先处理完毕，才能开启代支付功能。');
                        $scope.data.yjff.value = '0';
                        return;
                    }
                    paymentType(value);
                }

            };

            /**
             * @param {string} urlPath
             * @param {Object} data
             * @param {string} sucTip
             * @param {boolean=} updateData
             * @param {function=} sucCallback
             */
            function postData(urlPath, data, sucTip, updateData, sucCallback) {
                $http
                    .post(G_CONFIG.api_prefix + "qmyx/para-setting/" + urlPath, data)
                    .success(function (res) {
                        var retCode = res.retCode;
                        if (retCode == 0) {
                            if (updateData) {
                                getData();
                            }

                            sucCallback && sucCallback(res);

                            ui.showSucTip(sucTip);
                        } else {
                            ui.showErrTip(res.errMsg);
                        }
                    })
            }

            $scope.loading = false;
            /**
             * 获取数据
             * @param {string=} projectId
             */
            var getData = function () {
                $scope.loading = true;
                return $http
                    .get(api_prefix + "get-jieyong-manage")
                    .then(function (res) {
                        var retCode = res.data.retCode;
                        if (retCode == 0) {
                            var data = res.data.data;
                            $scope.data = data;

                        } else {
                            ui.showErrTip(res.data.errMsg);
                            return false;
                        }
                    })
                    .finally(function () {
                        $scope.loading = false;
                    })
            };


            getData().then(function () {
                //积分兑换开关
                ui.initSwitch("#exchange_btn", function (data) {
                    $scope.data.integral.instruction = $scope.data.integral.instruction || "把佣金转换为云服务的会员积分 。可在会员积分商城中兑换购买实惠商品，比拿现金划算哦！";
                    if (data.value==1 && $scope.data.YunParams && $scope.data.YunParams.value == 0) {
                        util.alert('开启佣金兑换积分需先对接第三方会员及积分系统，请联系云客运营咨询详情，安排开发对接');
                        ui.offSwitch("exchange_btn");
                        data.value = 0;
                        data.text = '关';
                        return;
                    }
                    if ($scope.data.integral) {
                        $scope.data.integral.enable = data.value;
                        $scope.$applyAsync();

                    }
                });
                if ($scope.data.YunParams && $scope.data.YunParams.value == 1 && $scope.data.integral && $scope.data.integral.enable == 1) {
                    ui.openSwitch("exchange_btn");
                } else {
                    ui.offSwitch("exchange_btn");
                }

            });

            //保存积分设置
            $scope.saveJFsettings = function () {
                var postData = {
                    rounding: $scope.data.integral.rounding,
                    exchangeWay: $scope.data.integral.exchangeWay,
                    proportion: $scope.data.integral.proportion,
                    instruction: $scope.data.integral.instruction,
                    enable: $scope.data.integral.enable,
                    id: $scope.data.integral.id
                };
                $http.post(api_prefix + "save-integral-data", postData)
                    .success(function (res) {
                        if (res.retCode == 0) {
                            ui.showSucTip();
                        } else {
                            ui.showErrTip(res.errMsg);
                        }
                    });

            }

        }])
});